# -*- coding: utf-8 -*-
import re
from google.appengine.api import urlfetch

import settings
import model.alert


def _regexParse(reg, vars, content, debug=False):
    reg = re.compile(reg, re.S)
    content = reg.findall(content)
    output = {}
    if content:
        if len(vars) > 1:
            content = content[0]
        for i in range(len(vars)):
            output[vars[i]] = content[i]
    return output

def _prepareContent(content):
    reg_items = re.compile('<!-- Сами записи: комменты -->(.*)<!-- //  Сами записи: комменты -->',
                           re.S)
    items = reg_items.search(content)
    if items:
        content = items.group(0)
    else:
        model.alert.signal('error', 'not found items in content')
    content = re.sub('<(/?)blockquote>', r'<\1q>', content)
    return content

def _prepareEntryContent(entry):
    if entry:
        # remove img
        reg_img = re.compile('<img.*src="(.*?)".*>')
        imgs = reg_img.findall(entry)
        if imgs:
            index = 0
            for img in imgs:
                entry = reg_img.sub('<a href="%s" class="lhc_img">[img%s]</a>' % (img, index),
                                    entry, 1)
                index += 1
        # not even count of tags
        reg_p = re.compile('</?p>')
        if len(reg_p.findall(entry))%2 == 1:
            entry = reg_p.sub('', entry)
        # ampersand "&" 
        entry = re.sub('&(?!amp;)', '&amp;', entry)
        # tag <u> 
        entry = re.sub('<u>', '<span class="underline">', entry)
        entry = re.sub('<\u>', '<\span>', entry)
    else:
        entry = ''
    return {'entry_content': entry}

def _parse(content):
    output = []
    content = _prepareContent(content)
    reg = re.compile('<table class="popular-comment">(.*?)</table>',  re.S)
    content = reg.findall(content)
    for row in content:
        row_dict = {}
        row_dict.update(_regexParse(
            # pattern
            '<td class="comment-descr".*?<dl>.*?<dt>' 
          + '<a class="where" href="(.*?)">(.*?)</a> / '
          + '<a class="topic" href="(.*?/([-_\w\d]+)/(\d+)/)">(.*?)</a>'
          + '</dt>.*?<dd>(\d+)</dd>.*'
          + '<li class="bookmark">.*?<a href=".*?#comment_(\d+)" title=".*?"'
          + ' rel="bookmark">#</a>.*'
          + '<li class="entry-content">(.*?)</li>',
            # variables
            ['where_url', 'where_text', 'topic_url',
             'where_id', 'topic_id', 'topic_text',
             'comments_count',
             'comment_id',
             'entry_content'],
            row))
        row_dict.update(_prepareEntryContent(row_dict['entry_content']))
        output.append(row_dict)
    return output

def getComments():
    result = urlfetch.fetch(settings.LIVEHABR_URL,
                            headers = {
                                'User-Agent': 'habrahabr-live-bot; http://www.livehabr.ru;'
                            },
                            allow_truncated=True)
    items = _parse(result.content) if result.status_code == 200 else []
    return {'items': items}
